/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyyoona7.picker.util;

import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;

/**
 * Attr util
 *
 * @author shetaotao
 * @since 2021-03-04
 */
public final class AttrUtil {
    private AttrUtil() {
    }

    /**
     * 获取资源值
     *
     * @param attrSet attr set
     * @param key resource key
     * @param defValue 默认值
     * @return 颜色
     */
    public static Color getColorValue(AttrSet attrSet, String key, String defValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getColorValue();
        } else {
            return new Color(Color.getIntColor(defValue));
        }
    }

    /**
     * 获取资源值
     *
     * @param attrSet attr set
     * @param key resource key
     * @param isDefValue 默认值
     * @return Boolean
     */
    public static boolean getBooleanValue(AttrSet attrSet, String key, boolean isDefValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getBoolValue();
        } else {
            return isDefValue;
        }
    }

    /**
     * 获取资源值
     *
     * @param attrSet attr set
     * @param key resource key
     * @param defValue 默认值
     * @return Element
     */
    public static Element getElementValue(AttrSet attrSet, String key, Element defValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getElement();
        } else {
            return defValue;
        }
    }

    /**
     * 获取资源值
     *
     * @param attrSet attr set
     * @param key resource key
     * @param defValue 默认值
     * @return String
     */
    public static String getStringValue(AttrSet attrSet, String key, String defValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getStringValue();
        } else {
            return defValue;
        }
    }

    /**
     * 获取资源值
     *
     * @param attrSet attr set
     * @param key resource key
     * @param defDimensionValue 默认值
     * @return int
     */
    public static int getDimension(AttrSet attrSet, String key, int defDimensionValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getDimensionValue();
        } else {
            return defDimensionValue;
        }
    }

    /**
     * 获取资源值
     *
     * @param attrSet AttrSet
     * @param key resource key
     * @param defValue 默认值
     * @return int
     */
    public static int getIntegerValue(AttrSet attrSet, String key, int defValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getIntegerValue();
        } else {
            return defValue;
        }
    }
}
